/****************************************************************************\
 * @file    game_flow.c
 * @author  马新硕
 * @date    2022/10/19
 * @brief   游戏的界面和流程管理
 * @note    切换不同界面和工作流
 ******************************************************************************
 * @attent  
 ****************************************************************************/
#include "game_flow.h"

uint8_t gameFlow = gameStart;//游戏工作流

uint8_t gameTansTime = 0;
uint8_t tansflag = 0;

static uint8_t gameLevelFinishFlag = 0;//关卡完成标志

static void gameLevelUpdate(void);

void gameInterfaceSwitch(uint8_t *flow)
{
  static uint8_t waitTime = 0;//开始界面的等待时间，时间到才能开始游戏
  static uint8_t gameLevelBuildFlag = 1;//关卡构建标志
  static uint8_t gameLevelcnt = 0;
  static uint8_t clear_Flag = 1;
  
  switch(*flow)
  {
    case gameStart://开始界面
      clear_Flag = 1;
      startGameSurfaceCreate();
      if(waitTime<=20){waitTime++;}
      if(JoyStick.ButtonA && waitTime>=15){*flow = gameLevel; waitTime = 0;}
      break;
    
    case gameLevel://闯关界面
      if(gameLevelBuildFlag){gameLevel_Init(&gameLevelcnt); gameLevelBuildFlag = 0;}
      gameLevelUpdate();
      if(gameLevelFinishFlag)//闯关结束的标志  
      {
        if(gameLevelcnt < 5)
        {
          *flow = gameTansfer;
          tansflag = 1;
        }
        else
        {
          ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
          gameLevelBuildFlag = 1;
          gameLevelcnt = 0;
          *flow = gameEnd;
        }
      }
      if(usersrole->Health == 0)//玩家闯关成功或死亡进入结束界面
      {
        ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
        gameLevelBuildFlag = 1;
        gameLevelcnt = 0;
        *flow = gameEnd;
      }
      break;
    
    case gameTansfer://传送
      TansferSurfaceCreate();
      if(gameTansTime == 5)
      {
        gameLevelcnt++;
        gameTansTime = 0;
        gameLevelBuildFlag = 1;
        gameLevelFinishFlag = 0;
        
        *flow = gameLevel;
      }
      break;
    
    case gameEnd://游戏结束界面
      if(clear_Flag)
      {
        LCD_SetColors(RED, BLACK);
        ILI9341_Clear(0, 0, LCD_X_LENGTH, LCD_Y_LENGTH);
        LCD_SetColors(RED, WHITE);
        clear_Flag = 0;
      }
      endGameSurfaceCreate();
      if(JoyStick.ButtonA)
      {
        usersrole->Health = usersrole->MaxHealth;
        usersrole->Shield = usersrole->MaxShield;
        LCD_SetColors(RED, BLACK);
        ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);//清屏
        LCD_SetColors(RED, WHITE);
        gameLevelFinishFlag = 0;
        *flow = gameStart;
      }
      break;
  }

}


static void gameLevelUpdate(void)
{
/****************玩家和怪物操作****************/
  usersControl();//玩家的动作
  usersShieldRecover();//玩家护盾回复
  enemies_Move();//敌人自主移动
	boss_Move();//Boss移动
  
/****************碰撞检测****************/
  users_and_mapblock_Crash();//玩家与地图方块的碰撞检测
  users_and_enemies_Crash();//玩家与敌人碰撞
  users_and_enemiesbullet_Crash();//玩家与敌人子弹碰撞
  usersbullet_and_mapblock_Crash();//玩家子弹与地图碰撞
  usersbullet_and_enemies_Crash();//玩家子弹与敌方单位碰撞
  usersattack_and_enemies_Crash();//近战武器与敌方单位碰撞
  //enemies_and_enemies_Crash();//敌人与敌人的碰撞
  enemies_and_mapblock_Crash();//敌人与地图方块的碰撞检测
  enemiesbullet_and_mapblock_Crash();//敌人子弹与地图碰撞
  
/****************更新画面****************/
  map_and_userscondition_Update();//更新地图信息
  users_Update();//更新玩家信息
  bullet_Update();//更新子弹信息
  enemies_Update();//更新敌人信息
  
/****************闯关结束条件判断****************/
  if(endGameLevelCheck()){gameLevelFinishFlag = 1;}//TODO 完善闯关结束标志的判断条件
}
